home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_353 / northc / northc1.lzh / libs / crt0.asm < prev    next >
Assembly Source File  |  1990-04-14  |  4KB  |  225 lines

  1. ;  (c) 1990 S.Hawtin.
  2. ;  Permission is granted to copy this file provided
  3. ;   1) It is not used for commercial gain
  4. ;   2) This notice is included in all copies
  5. ;   3) Altered copies are marked as such
  6. ;
  7. ;  No liability is accepted for the contents of the file.
  8. ;
  9. ;
  10. ; C startup file for the Amiga.
  11. ;
  12. ; This file must be linked as the first object module in any C program.
  13. ; it loads all the needed libraries, then calls __main to set up the
  14. ; command line parameters and the default FILE * structures.
  15. ;
  16. ;  CHANGES
  17. ;
  18. ;    12 Dec 1989    Created as simple startup        S.Hawtin
  19. ;    17 Feb 1990    Added workbench drivers            S.Hawtin
  20. ;    13 Apr 1990    Changed AmigaDOS library interface    S.Hawtin
  21. ;
  22. ;        Macro definitions
  23. ;
  24.     INCLUDE    clibdefs.i
  25. ;
  26. ;
  27. ;
  28.     section ONE
  29. ;
  30. ;
  31. ;
  32.     XREF    __main
  33.     XREF    initmem
  34.     XREF    clearmem
  35.     XREF    fp_open
  36.     XREF    fp_close
  37.     XREF    __WBConsole
  38.     XREF    __AmiLibArray
  39.     XREF    finalLib
  40.  
  41. ;
  42. ;        Define entry point
  43. ;
  44.        XDEF    start
  45. start:
  46.     move.l    sp,StkPtr    ; Save stack pointer for EXIT
  47.     move.l    d0,__cmndlen    ; Save length of command
  48.     move.l    a0,__cmndstr    ; Save command buffer
  49. ;
  50. ;        Open DOS library
  51. ;
  52.     move.l    #dosname,a1
  53.     clr       d0
  54.     call    exec,OpenLibrary
  55.     move.l    d0,dosLib
  56.     tst.l    d0
  57.     beq    error_exit
  58. ;
  59. ;    Set up malloc lists
  60. ;
  61.     jsr    initmem
  62.     tst.l    d0
  63.     beq    error_exit
  64. ;
  65. ;    Open the floating point library.  The mathtrans library is only 
  66. ;       opened when it is required.
  67. ;
  68.     jsr    fp_open
  69. ;
  70. ;    Were we started from the workbench ?
  71. ;
  72.     move.l    #0,a1        ; Find this tasks structure
  73.     call    exec,FindTask
  74.     move.l    d0,Task
  75.     move.l    d0,a2
  76.     tst.l    PROC.CLI(a2)    ; Is it a CLI task?
  77.     beq.s    WBtask
  78. ;
  79. ;        Find standard file handles
  80. ;
  81. CLItask:
  82. ; This section has been removed because I cannot seem to find the
  83. ; command name.
  84. ;    ; Get the command name here
  85. ;    move.l    PROC.CLI(a2),a0
  86. ;    add.l    a0,a0
  87. ;    add.l    a0,a0        ; Now points to CLI struct
  88. ;    move.l    CLI.COMMAND(a0),a1
  89. ;    add.l    a0,a0
  90. ;    add.l    a0,a0        ; Now points to command name
  91. ;    moveq.l    #0,d0
  92. ;    move.b    (a0)+,d0
  93. ;    move.l    d0,__cmndnlen
  94. ;    move.l    (a0),__cmndnam
  95.     ; Now get the input and output handles
  96.     move.l    #0,__fromWB    ; Not from workbench
  97.     call    dos,Input
  98.     move.l    d0,__stdin
  99.     beq    error_exit
  100.     call    dos,Output
  101.     move.l    d0,__stdout
  102.     beq    error_exit
  103.     bra    NowRun
  104. ;
  105. ;    Called from the workbench
  106. ;
  107. WBtask:
  108.     move.l    #-1,__fromWB    ; from Workbench
  109.     lea    PROC.MSGPORT(a2),a0
  110.     call    exec,WaitPort    ; wait for the workbench message
  111.     lea    PROC.MSGPORT(a2),a0
  112.     call    exec,GetMsg
  113.     move.l    d0,WBmsg
  114.     ; Find the directory the program is in
  115.     move.l    d0,a2
  116.     move.l    SM.ARGLIST(a2),d0
  117.     beq    noDir
  118.     move.l    d0,a0
  119.     move.l    WA.LOCK(a0),d1
  120.     call    dos,CurrentDir
  121. noDir:
  122.     move.l    __WBConsole,d1    ; Default console
  123.     move.l    #MODE_NEWFILE,d2
  124.     call    dos,Open
  125.     move.l    d0,__stdin    ; Console becomes default
  126.     move.l    d0,__stdout
  127.     beq    error_exit
  128.     ; Convert open console into 68000 address
  129.     lsl.l    #2,d0
  130.     move.l    d0,a0
  131.     move.l    Task,a2        ; Link the console to the task
  132.     move.l    FH.TYPE(a0),PROC.CONSOLE(a2)
  133. ;
  134. ;    Call main C entry point via Cstartup
  135. ;
  136. NowRun:
  137.     bsr    __main
  138. ;
  139. ;    If _main returns fall through to a normal (0) exit
  140. ;
  141.     move.l    #0,a0
  142.  
  143. a0_exit:
  144.     move.l    a0,-(sp)        ; Hide the return code
  145.     jsr    clearmem        ; Tidy up memory
  146.  
  147.     tst.l    __fromWB
  148.     beq    exit1
  149.     move.l    __stdout,d1        ; If stdout is still open, close it
  150.     beq    exit1
  151.     call    dos,Close
  152. exit1:
  153.     tst.l    WBmsg
  154.     beq.s    exit2
  155.     call    exec,Forbid        ; Must do this before ReplyMsg
  156.     move.l    WBmsg,a1        ; or risk vanishing
  157.     call    exec,ReplyMsg
  158. exit2:
  159.     ; Close any libraries that have been left open
  160.     move.l    #finalLib,a2
  161. exit3:
  162.     subq.l    #4,a2
  163.     move.l    (a2),a1
  164.     cmp.l    #0,a1
  165.     beq    exit4
  166.     call    exec,CloseLibrary
  167. exit4:
  168.     cmp.l    #__AmiLibArray,a2
  169.     bne    exit3
  170.     move.l    (sp)+,d0        ; Return code stored in d0
  171.     rts
  172. ;
  173. ;     Close with an error, we have no clue as to which one
  174. ;
  175. error_exit:
  176.     move.l    #100,a0            ; 100 seems like a nice error code
  177.     bra    a0_exit
  178.  
  179. ;
  180. ;    void _exit(long num);
  181. ;
  182.     xdef    __exit
  183. __exit:
  184.     move.l    4(sp),a0    ; Put the arg into a0
  185.     move.l    StkPtr,sp    ; Restore the stack
  186.     bra    a0_exit        ; and exit
  187.  
  188. ;
  189.     section  TWO,data
  190. ;
  191. ;    DOS Library name
  192. ;
  193. dosname dc.b    'dos.library',0
  194. ;
  195.          section    THREE,bss
  196. ;
  197. ;
  198. ;
  199. CharBuff ds.b      1
  200.      ds.b     1
  201. ;
  202. ;
  203. ;
  204. StkPtr     ds.l      1 
  205. ;
  206. ;
  207. ;
  208.     xdef    __cmndlen,__cmndstr,__cmndnam,__cmndnlen
  209. __cmndnlen ds.l      1     ;Command name length
  210. __cmndnam ds.l      1    ;Command name
  211. __cmndlen ds.l      1    ;Command line length
  212. __cmndstr ds.l      1    ;Command line buffer
  213.  
  214.     xdef    __stdin,__stdout
  215. __stdin  ds.l     1     ;input file handle
  216. __stdout ds.l     1     ;output file handle
  217. ;
  218. ;
  219.     xdef    __fromWB
  220. __fromWB ds.l    1    ; Zero if called from CLI
  221. Task    ds.l    1    ; This tasks structure
  222. WBmsg    ds.l    1    ; message from WB
  223.  
  224.     end
  225.